// pages/binding/binding.js
const app = getApp()
var promise = require('../../utils/request.js');

Page({
  data: {

  },
  onLoad() {
    let that = this
    let unionId = wx.getStorageSync('unionId')
    if (unionId) {} else {
      that.check()
    }
  },
  formSubmit: function(e) {
    console.log(e)
    this.resister(e);
  },
  resister: function(e) {
    var data = e.detail.value;
    data.unionId = wx.getStorageSync('getImportantUnionid');
    data.type = 1;
    var userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      data.nickName = userInfo.nickName;
      data.headImg = userInfo.avatarUrl;
    }
    var header = {
      'content-type': 'application/json;charset=UTF-8'
    }
    promise.notAuthorizeRequest("operator/register", 'POST', data, header, 4, {
      success: function(res) {
        console.log(res)
        if (res === true) {
          promise.tLogin(data.unionId)
          wx.switchTab({
            url: '../home/home'
          })
        }
      }
    }, {
      fail: function(res) {
        console.log(res)
        promise.toastError(res);
      }
    }, {
      complete: function() {

      }
    })
  },
  check() {
    wx.getSetting({
      success(res) {
        console.log(res)
        if (res.authSetting['scope.userInfo']) {
          // console.log(res)
        } else {
          wx.navigateTo({
            url: '../authorization/authorization',
          })
        }
      }
    })
  }
})